package com.wing.productionPlan.service;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.wing.common.enums.ProductEnum;
import com.wing.productionPlan.dao.DemandPlanDao;
import com.wing.productionPlan.model.entity.DemandPlan;
import com.wing.productionPlan.model.entity.DemandPlanRecord;
import com.wing.productionPlan.model.response.CustomerCataLogVo;
import com.wing.productionPlan.model.response.DemandPlanCollectionVO;
import com.wing.productionPlan.model.response.DemandPlanVO;

import com.wing.web.excel.DemandPlanExcelln;
import com.wing.web.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 需求计划表-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class DemandPlanServiceImpl extends ServiceImpl<DemandPlanDao, DemandPlan> implements DemandPlanService {

    @Autowired
    private DemandPlanRecordService demandPlanRecordService;


    /**
     * 分页查询任务列表
     *
     * @param pg
     * @param map
     * @return
     */
    @Override
    public Page<DemandPlanVO> pageList4app(Page<DemandPlanVO> pg, Map map) {
        List<DemandPlanVO> vos = this.baseMapper.pageList4app(pg, map);
        pg.setRecords(vos);
        return pg;
    }

    @Override
    public DemandPlanVO get4appById(Long id) {
        return this.baseMapper.get4appById(id);
    }

    /**
     * 导入
     */
    @Transactional(readOnly = false)
    public void importExcel(List<DemandPlanExcelln> list) {
        try {
            addExcel(list);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<CustomerCataLogVo> getCustomerInfoAndSkuInfo() {
        return this.baseMapper.getCustomerInfoAndSkuInfo();
    }

    /**
     * 需求计划汇总
     * @param pg
     * @param map
     * @return
     */
    @Override
    public Page<DemandPlanCollectionVO> pageListCollection4app(Page<DemandPlanCollectionVO> pg, Map map) {
        List<DemandPlanCollectionVO> vos = this.baseMapper.pageListCollection4app(pg, map);
        pg.setRecords(vos);
        return pg;
    }


    /**
     * 添加excel
     *
     * @param list
     */
    @Transactional
    public void addExcel(List<DemandPlanExcelln> list) {

        //处理数据
        HashSet<DemandPlanExcelln> demandPlanExcellns = new HashSet<>(list);

        //获取客户下的sku产品信息
        List<CustomerCataLogVo> customerInfoAndSkuInfo = this.getCustomerInfoAndSkuInfo();

        //根据客户名称 分组
        Map<String, List<CustomerCataLogVo>> vos = customerInfoAndSkuInfo.stream().collect(Collectors.groupingBy(CustomerCataLogVo::getCustomerName));

        List<DemandPlan> plans = new ArrayList<>();
        demandPlanExcellns.forEach(info -> {
            List<CustomerCataLogVo> customerCataLogVos = vos.get(info.getFactoryName());
            if (!CollectionUtil.isEmpty(customerCataLogVos)) {
                for (CustomerCataLogVo customerCataLogVo : customerCataLogVos) {
                    if (customerCataLogVo.getSkuCode().equals(info.getSkuCode())){
                        DemandPlan demandPlan = new DemandPlan();
                        List<DemandPlanRecord> records = new ArrayList<>();
                        BeanUtils.copyProperties(info, demandPlan);
                        demandPlan.setCustomerProductId(customerCataLogVo.getCustomerProductId());
                        demandPlan.setCustomerId(customerCataLogVo.getCustomerId());
                        demandPlan.setCreator(JwtUtils.getLoginUserId());
                        demandPlan.setMaterialTypeId(ProductEnum.DemandPlanTypeEmum.getCodeByName(info.getMaterialType()));
                        demandPlan.setModifier(JwtUtils.getLoginUserId());
                        demandPlan.setCreateTime(LocalDateTime.now());
                        demandPlan.setCreatorName(JwtUtils.getLoginUsername());
                        list.forEach(item -> {
                            if (item.equals(info)) {
                                DemandPlanRecord demandPlanRecord = new DemandPlanRecord();
                                demandPlanRecord.setPlanTime(item.getPlanTime());
                                demandPlanRecord.setPlanNum(item.getDemandNumber());
                                demandPlanRecord.setCreator(JwtUtils.getLoginUserId());
                                demandPlanRecord.setModifier(JwtUtils.getLoginUserId());
                                demandPlanRecord.setCreateTime(LocalDateTime.now());
                                demandPlanRecord.setCreatorName(JwtUtils.getLoginUsername());
                                records.add(demandPlanRecord);
                            }
                        });
                        demandPlan.setPlanRecords(records);
                        plans.add(demandPlan);
                    }
                }
            }
        });

        //导入数据
        for (DemandPlan item : plans) {
            save(item);
            List<DemandPlanRecord> planRecords = item.getPlanRecords();
            planRecords.forEach(info->{
                info.setDemandPlanId(item.getId());
            });
            demandPlanRecordService.saveBatch(planRecords);
        }
    }
}

